import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import './assets/css/global.css'
import './assets/fonts/iconfont.css'
import axios from 'axios'
import TreeTable from 'vue-table-with-tree-grid'
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css' // import styles
import 'quill/dist/quill.snow.css' // for snow theme
import 'quill/dist/quill.bubble.css' // for bubble theme
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 配置请求的根路径
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'
// 每个请求都必须拦截 查看是否有token 设置在请求头里
axios.interceptors.request.use(config => {
  // 在每次请求的request拦截器中 使用NProgress展示进度条 NProgress.start()
  NProgress.start()
  config.headers.Authorization = window.sessionStorage.getItem('token')
  // 必须return config
  return config
})

// 在response拦截器中 隐藏NProgress的进度条 NProgress.done()
// .use里指定一个回调函数 拿到config配置对象以后 一定要return config
axios.interceptors.response.use(config => {
  NProgress.done()
  return config
})

// 设置在这里 其他模块里都可以直接$http发送请求
Vue.prototype.$http = axios

Vue.component('tree-table', TreeTable)

Vue.config.productionTip = false

Vue.use(VueQuillEditor)

Vue.filter('dateFormat', function(originVal) {
  const dt = new Date(originVal)
  const year = dt.getFullYear()
  const month = (dt.getMonth() + 1 + '').padStart(2, '0')
  const date = (dt.getDate() + '').padStart(2, '0')

  const hh = (dt.getHours() + '').padStart(2, '0')
  const min = (dt.getMinutes() + '').padStart(2, '0')
  const sec = (dt.getSeconds() + '').padStart(2, '0')
  return `${year}-${month}-${date} ${hh}:${min}:${sec}`
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
